package com.landeb.shop.wxapi;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.landeb.shop.huo.bean.BeanWXPayRequest;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    public static final String WX_APPID = "wx8463fa730609a589";


    public static void WXPay(Context context, BeanWXPayRequest.DataBean wxObj) {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        IWXAPI api = WXAPIFactory.createWXAPI(context, WX_APPID);
        // 将该app注册到微信
        api.registerApp(WX_APPID);

//        String textData = "{\"appid\":\"wxb4ba3c02aa476ea1\",\"partnerid\":\"1305176001\",\"package\":\"Sign=WXPay\",\"noncestr\":\"bd9c7ee35012eadb7b57e1b4538bd504\",\"timestamp\":1479870076,\"prepayid\":\"wx20161123110116730431f12e0888450039\",\"sign\":\"1B7A0DB084B3FF73C3DC8E46F250A9C1\"}";
//        String textData = "{\"appid\":\"wx8463fa730609a589\",\"partnerid\":\"1487030302\",\"prepayid\":\"wx2017092510262477fac795720790038192\",\"package\":\"Sign=WXPay\",\"noncestr\":\"ZZXSx3CI4s0GJ0KL\",\"timestamp\":\"1503971305\",\"sign\":\"21816E401F3FAE9450DF3F5F76392C86\"}";



//        try {

            PayReq req = new PayReq();


            req.appId = WX_APPID;
            req.partnerId = wxObj.getPartnerid();
            req.prepayId = wxObj.getPrepayid();
            req.nonceStr = wxObj.getNoncestr();
            req.timeStamp =wxObj.getTimestamp();
            req.packageValue ="Sign=WXPay";
            req.sign = wxObj.getSign();

//            JSONObject json = new JSONObject(textData);
//            req.appId = json.getString("appid");
//            req.partnerId = json.getString("partnerid");
//            req.prepayId = json.getString("prepayid");
//            req.nonceStr = json.getString("noncestr");
//            req.timeStamp = json.getString("timestamp");
//            req.packageValue = json.getString("package");
//            req.sign = json.getString("sign");


//            req.extData = "app data"; // optional

            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
            api.sendReq(req);
//        } catch (Exception e) {
//
//            Log.e( "WXPay: ",e.getMessage() );
//            e.printStackTrace();
//        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


/**       未加载layout*/
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, WX_APPID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d("onWXPayFinish", "errCode = " + resp.errCode);

        /**
         * 支付回调
         * */

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle(R.string.app_tip);
//            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//            builder.show();

            if (resp.errCode == 0) {
                Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {

                Log.e( "onResp: ",resp.errCode+"----------"+resp.errStr );

                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
                builder.setTitle("系统提示").setCancelable(false).setMessage("支付失败，请重试")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).show();
            }

        }
    }
}